package cn.iocoder.yudao.module.scrm.service.statistics;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.module.scrm.controller.admin.approve.vo.ApproveReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.approve.vo.ApproveRespVO;
import cn.iocoder.yudao.module.scrm.controller.admin.statistics.vo.ScrmStatisticsDataReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.statistics.vo.ScrmStatisticsDataRespVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.customer.CustomerDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.customer.customerrepository.CustomerRepositoryRuleDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.invoice.InvoiceDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.receivable.ReceivableDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.refund.RefundDO;
import cn.iocoder.yudao.module.scrm.dal.mysql.customer.CustomerMapper;
import cn.iocoder.yudao.module.scrm.dal.mysql.customer.customerrepository.CustomerRepositoryRuleMapper;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.concurrent.TimeUnit;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.scrm.enums.common.ScrmStatisticsDataTypeEnum.getEnumByType;


/**
 * CRM 排行榜统计 Service 实现类
 *
 * @author anhaohao
 */
@Service
@Slf4j
public class ScrmStatisticsDataServiceImpl implements ScrmStatisticsDataService {

    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private DeptApi deptApi;
    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private CustomerRepositoryRuleMapper customerRepositoryRuleMapper;





    /**
     * 获得各个业务模块数量统计
     * @param dataReqVO type区分模块
     * @return 返回
     */
    @Override
    public ScrmStatisticsDataRespVO getCountByType(ScrmStatisticsDataReqVO dataReqVO) {
        Integer type = dataReqVO.getType();
        log.info("获得模块数量统计类型: {}", dataReqVO.getType());
        ScrmStatisticsDataRespVO respVO = new ScrmStatisticsDataRespVO();
        Long loginUserId = getLoginUserId();
        AdminUserRespDTO userRespDTO = adminUserApi.getUserNotCheckDataPermission(loginUserId).getCheckedData();
        Long deptId = userRespDTO.getDeptId();
        respVO.setName(userRespDTO.getNickname());

        switch (getEnumByType(type)) {
            case SCRM_STATISTICS_COUNT_CLUE:
                break;
            case SCRM_STATISTICS_COUNT_CUSTOMER:
                respVO = getStatisticsCountByType(customerMapper, CustomerDO::getOwnerUserId,loginUserId,deptId,type,respVO);
                break;
            default:
                return new ScrmStatisticsDataRespVO();
        }
        log.info("静态数据统计完成，操作人ID: {} 返回结果: {}", loginUserId,respVO);
        return respVO;
    }


    private <T> ScrmStatisticsDataRespVO getStatisticsCountByType(
                                           BaseMapper<T> mapper,
                                           SFunction<T, ?> ownerUserIdGetter,
                                           Long loginUserId,
                                           Long deptId,
                                           Integer type,
                                            ScrmStatisticsDataRespVO respVO) {


        //返回个人拥有的数量
            Long count = mapper.selectCount(new LambdaQueryWrapper<T>()
                    .eq(loginUserId != null, ownerUserIdGetter, loginUserId)
                    .select());
            respVO.setCount(count);



            //为不同的业务处理不同的数量
            switch (getEnumByType(type)) {
                case SCRM_STATISTICS_COUNT_CLUE:
                     break;
                case SCRM_STATISTICS_COUNT_CUSTOMER:
                    QueryWrapper<CustomerDO> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("lock_status",true);
                    queryWrapper.eq("owner_user_id",loginUserId);
                    Long lockCount = customerMapper.selectCount(queryWrapper);
                    respVO.setLockCount(lockCount);
                    if (deptId != null){
                        CustomerRepositoryRuleDO repositoryRuleByDeptId = customerRepositoryRuleMapper.getRepositoryRuleByDeptId(deptId);
                        //获取到的客户库规则
                        if (repositoryRuleByDeptId!=null){
                            //规则的锁定数量
                            Integer customerCountLock = repositoryRuleByDeptId.getCustomerCountLock();
                            //有规则，剩余锁定数量等于规则的锁定数量减去已经锁定的数量
                            respVO.setRemainLockCount(String.valueOf(customerCountLock-lockCount));

                        }else {
                            //没有规则，剩余默认为无限
                            respVO.setRemainLockCount("--");
                        }
                    }else {
                        //没有部门，剩余默认为无限
                        respVO.setRemainLockCount("--");
                    }
                     break;
                default:
                    return new ScrmStatisticsDataRespVO();
            }


        return respVO;
    }


}